﻿<?php

/**
 * 
 * DB操作
 *
 */
class DBClass{
	
	private $PDO;
	
	private $SpecailTarget_Left;
	private $SpecailTarget_Right;
	private $PreviewTable;
	
	static function getInstance(){
		static $s;
		if(!isset($s)){
			$s=new DBClass();
			$c=Config::getInstance();
			$s->PDO=new PDO($c->DBDNS,$c->DBID,$c->PASSWORD);
			$s->SpecailTarget_Left=$c->DB_SpecailTarget_Left;
			$s->SpecailTarget_Right=$c->DB_SpecailTarget_Right;
			$s->PreviewTable=$c->DB_PreviewTable;
		}
		return ($s);
	}
	
	/***
	 * @return new PDO;
	 * 
	 */
	function pdo(){
		return $this->PDO;
	}
	
	function query($query,$par=null){
		if($par==null){
			return $this->PDO->query($query);
		}else{
			$sth=$this->POD->prepare($query);
			$sth->excute($par);
			return $sth;
		}
	}
	
	/**
	 * 
	 * @param string $tablename
	 * @param array $dataarray
	 * @return int
	 */

	function insert($tablename='',$dataarray=array()){
		try{
			$arrkey=array();$arrval=array();
			foreach($dataarray as $k => $v){
				$arrkey[$this->SpecailTarget_Left.$k.$this->SpecailTarget_Right]=':'.$k;
				$arrval[':'.$k]=$v;
			}
			$sth=$this->PDO->prepare('insert into ['.$this->PreviewTable.$tablename.']('.implode(',',array_keys($arrkey)).')values('.implode(',',array_values($arrkey)).');');

			$sth->execute($arrval);
		 	
			return $sth->rowCount();
		}catch(PDOException $e){
			return -1;
		}
	}
	
	//table name,condition,orderby,limit
	/**
	 * @param string $tablename
	 * @param array $datacondition
	 * @param string $dataorderby
	 * @param array $datalimit
	 * @param array $datacolumns
	 * @return array |NULL
	 */
	function select($tablename='',$datacondition=array(),$dataorderby=null,$datalimit=null,$datacolumns=array('*')){
		try{
			$arrkey=array();$arrval=array();
			
			foreach($datacondition as $k => $v){
				if(is_array($v)){
					$arrkey[]=$this->SpecailTarget_Left.$v[0].$this->SpecailTarget_Right.$v[1].'?';
					$arrval[]=$v[2];
				}else{
					$arrkey[]=$this->SpecailTarget_Left.$k.']=?';
					$arrval[]=$v;
				}
			}
			
			$sth=$this->PDO->prepare('select '.implode(',', $datacolumns).' from ['.$this->PreviewTable.$tablename.$this->SpecailTarget_Right.
					(count($datacondition)>0?' where '.implode(' and ',$arrkey):'').
					($dataorderby!=null?' order by '.$dataorderby:'').
					(count($datalimit)>0?' limit '.implode(',',$datalimit):'').';'
					);
			
			$sth->execute($arrval);
			return $sth->fetchAll(PDO::FETCH_ASSOC);
		}catch(PDOException $e){
			return $e;
		}
	}
	
	/**
	 * 
	 * @param string $tablename
	 * @param array $dataarray
	 * @param array $datacondition
	 * @return boolean|PDOException
	 */
	function update($tablename,$dataarray=array(),$datacondition=array()){
		try{
			$k=array();$c=array();$v=array();
			if(count($dataarray)==0) return false;
			foreach($dataarray as $i=>$j){
				$k[]=$this->SpecailTarget_Left.$i.']=?';
				$v[]=$j;
			}
			foreach($datacondition as $i=>$j){
				if(is_array($j)){
					$c[]=$this->SpecailTarget_Left.$j[0].$this->SpecailTarget_Right.$j[1].'?';
					$v[]=$j[2];
				}else{
					$c[]=$this->SpecailTarget_Left.$i.']=?';
					$v[]=$j;
				}
			}
			
			$sth= $this->PDO->prepare('update ['.$this->PreviewTable.$tablename.'] set '.implode(',',$k).(count($c)>0?' where '.implode(' and ',$c):'').';');
			$sth->execute($v);
			
			return $sth->rowCount();
		}catch(PDOException $e){
			return -1;
		}
	}
	
	
	/**
	 * 
	 * @param string $tablename
	 * @param array $datacondition
	 * @return int|PDOException
	 */
	function delete($tablename,$datacondition=array()){
		try{
			$condkey=array();$condval=array();
			foreach($datacondition as $k => $v){
				$condkey[$k]=$k.'=:'.$k;
				$condval[':'.$k]=$v;
			}
			$sth=$this->PDO->prepare('delete from ['.$this->PreviewTable.$tablename.$this->SpecailTarget_Right.(count($datacondition)>0?'where '.implode(',',$condkey):'').';');
			$sth->execute($condval);
		 	return $sth->rowCount();
		}catch(PDOException $e){
			return -1;
		}
	}
	
	/**
	 * @return string
	 */
	function guid(){
		return md5(uniqid(true,true));
	}
	function safe_datetime($date){
		if(is_array($date))
			$date=$date[0];
		return date('Y-m-d H:i:s',$date);
	}
	
	function parse_datetime($date){
		return $date;
	}
}
?>
